package com.hj.dapp.common.util;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.hj.dapp.business.ocr.model.OcrVo;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.graphics.image.LosslessFactory;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.springframework.core.io.ClassPathResource;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.NoSuchAlgorithmException;
import java.util.Objects;

/**
 * @author 胡奥程
 * @version 1.0
 * @date 2023/12/29
 */
public class Test {

    public static String PATH  ="C:\\Users\\Administrator\\Desktop\\装修轻辅合同.pdf";
    public static String PNG_PATH="C:\\Users\\Administrator\\Pictures\\id.jpg";
    public static String OUT_PATH="C:\\Users\\Administrator\\Desktop\\装修轻辅合同1.pdf";



    public static void main(String[] args) throws Exception {
        String base64FileStr = FileUtils.readFileToString(new File("C:\\Users\\Administrator\\Desktop\\1.txt"), "UTF-8");
        String img = Base64Util.rotateImage(base64FileStr, 270);
        //        1、获得PDF文件流对象
        FileInputStream pdfInputStream = new FileInputStream(PATH);
//        2、得到PDF文档对象
        PDDocument pdDocument = PDDocument.load(pdfInputStream);
//        3、由Document得到Page对象
        PDPage page = pdDocument.getPage(0);
//        4、通过图片路径和PDF文档对象得到图片image对象

        PDImageXObject image = LosslessFactory.createFromImage(pdDocument,Base64Util.base64ToBufferedImage(img));
//        5、创建pageStream对象
        PDPageContentStream pageStream = new PDPageContentStream(pdDocument, page,PDPageContentStream.AppendMode.APPEND,false,false);


//   6、pageStream对象绘制图片位置及大小，已PDF文件右下角为原点（x,y）是图片左下角左边，width、height是图片的长和宽
        pageStream.drawImage(image, 153, 642,70,30);

        pageStream.close();
//    7、保存PDF到指定路劲
        pdDocument.save(OUT_PATH);
//      8、关闭流
        pdDocument.close();
        pdfInputStream.close();



    }
}
